﻿namespace ManagerClinic.TiepNhan
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Ultilities.Ultility;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using System.Linq;
    using ManagerClinic.HoSoKhamBenh;

    public partial class frmCapNhatDmBenhNhan : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler ButtonClicked = null;
        int State = 0;

        DmBenhNhanBusiness dmBenhNhanBusiness = new DmBenhNhanBusiness();
        DmBenhNhanEntity dmBenhNhanEntity = null;

        DmDanTocBusiness dmDanTocBusiness = new DmDanTocBusiness();
        DmGioiTinhBusiness dmGioiTinhBusiness = new DmGioiTinhBusiness();
        DmNgheNghiepBusiness dmNgheNghiepBusiness = new DmNgheNghiepBusiness();
        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
        DmTinhThanhBusiness dmTinhThanhBusiness = new DmTinhThanhBusiness();
        DmQuanHuyenBusiness dmQuanHuyenBusiness = new DmQuanHuyenBusiness();

        Form parentForm = null;

        void setMaBenhNhan()
        {
            txtMaBenhNhan.Text = ConstantManagerClinic.setCodeManagerClinic("BN");
        }

        public frmCapNhatDmBenhNhan(int state, Form frm)
        {
            InitializeComponent();

            parentForm = frm;
            setMaBenhNhan();
            State = state;

            LoadDmGioiTinh();
            LoadDmDanToc();
            LoadDmNgheNghiep();
            LoadDmQuocGia();
            LoadDmTinhThanh();
            LoadDmQuocGia();
        }

        public frmCapNhatDmBenhNhan(int state, DmBenhNhanEntity obj, Form frm)
        {
            InitializeComponent();

            parentForm = frm;
            State = state;

            dmBenhNhanEntity = obj;

            LoadDmGioiTinh();
            LoadDmDanToc();
            LoadDmNgheNghiep();
            LoadDmQuocGia();
            LoadDmTinhThanh();
            LoadDmQuocGia();

            txtMaBenhNhan.Text = dmBenhNhanEntity.MaBenhNhan;
            txtTenBenhNhan.Text = dmBenhNhanEntity.TenBenhNhan;
            txtNgaySinh.Text = dmBenhNhanEntity.NgaySinh;
            txtThangSinh.Text = dmBenhNhanEntity.ThangSinh;
            txtNamSinh.Text = dmBenhNhanEntity.NamSinh.ToString();
            lkGioiTinh.EditValue = dmBenhNhanEntity.MaGioiTinh;
            lkDanToc.EditValue = dmBenhNhanEntity.MaDanToc;
            lkNgheNghiep.EditValue = dmBenhNhanEntity.MaNgheNghiep;
            lkQuocGia.EditValue = dmBenhNhanEntity.MaQuocGia;
            lkTinhThanh.EditValue = dmBenhNhanEntity.MaTinhThanh;
            lkQuanHuyen.EditValue = dmBenhNhanEntity.MaQuanHuyen;
            txtThonPho.Text = dmBenhNhanEntity.ThonPho;
            txtNoiLamViec.Text = dmBenhNhanEntity.NoiLamViec;
            txtDienThoai.Text = dmBenhNhanEntity.SoDienThoai;
            txtEmail.Text = dmBenhNhanEntity.Email;
        }

        void LoadDmGioiTinh()
        {
            var lstDmGioiTinhEntity = dmGioiTinhBusiness.GetDmGioiTinhList();
            var dmGioiTinhEntity = new DmGioiTinhEntity()
            {
                MaGioiTinh = ConstantManagerClinic.sValueMember,
                TenGioiTinh = ConstantManagerClinic.sDisplayMember
            };
            lstDmGioiTinhEntity.Insert(0, dmGioiTinhEntity);

            lkGioiTinh.Properties.DataSource = lstDmGioiTinhEntity;
            lkGioiTinh.Properties.DisplayMember = "TenGioiTinh";
            lkGioiTinh.Properties.ValueMember = "MaGioiTinh";
            lkGioiTinh.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmDanToc()
        {
            var lstDmDanTocEntity = dmDanTocBusiness.GetDmDanTocList();
            var dmDanTocEntity = new DmDanTocEntity()
            {
                MaDanToc = ConstantManagerClinic.sValueMember,
                TenDanToc = ConstantManagerClinic.sDisplayMember
            };
            lstDmDanTocEntity.Insert(0, dmDanTocEntity);

            lkDanToc.Properties.DataSource = lstDmDanTocEntity;
            lkDanToc.Properties.DisplayMember = "TenDanToc";
            lkDanToc.Properties.ValueMember = "MaDanToc";
            lkDanToc.EditValue = ConstantManagerClinic.sValueMember;

        }

        void LoadDmNgheNghiep()
        {
            var lstDmNgheNghiepEntity = dmNgheNghiepBusiness.GetDmNgheNghiepList();
            var dmNgheNghiepEntity = new DmNgheNghiepEntity()
            {
                MaNgheNghiep = ConstantManagerClinic.sValueMember,
                TenNgheNghiep = ConstantManagerClinic.sDisplayMember
            };
            lstDmNgheNghiepEntity.Insert(0, dmNgheNghiepEntity);

            lkNgheNghiep.Properties.DataSource = lstDmNgheNghiepEntity;
            lkNgheNghiep.Properties.DisplayMember = "TenNgheNghiep";
            lkNgheNghiep.Properties.ValueMember = "MaNgheNghiep";
            lkNgheNghiep.EditValue = ConstantManagerClinic.sValueMember;

        }

        void LoadDmQuocGia()
        {
            var lstDmQuocGiaEntity = dmQuocGiaBusiness.GetDmQuocGiaList();
            var dmQuocGiaEntity = new DmQuocGiaEntity()
            {
                MaQuocGia = ConstantManagerClinic.sValueMember,
                TenQuocGia = ConstantManagerClinic.sDisplayMember
            };
            lstDmQuocGiaEntity.Insert(0, dmQuocGiaEntity);

            lkQuocGia.Properties.DataSource = lstDmQuocGiaEntity;
            lkQuocGia.Properties.DisplayMember = "TenQuocGia";
            lkQuocGia.Properties.ValueMember = "MaQuocGia";
            lkQuocGia.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmTinhThanh()
        {
            var lstTinhThanhEntity = dmTinhThanhBusiness.GetDmTinhThanhList();

            if (lstTinhThanhEntity.Count > 0 && lkQuocGia.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstTinhThanhEntity = lstTinhThanhEntity.Where(n => n.MaQuocGia == lkQuocGia.EditValue.ToString()).ToList();
            }

            var dmTinhThanhEntity = new DmTinhThanhEntity()
            {
                MaTinhThanh = ConstantManagerClinic.sValueMember,
                TenTinhThanh = ConstantManagerClinic.sDisplayMember
            };
            lstTinhThanhEntity.Insert(0, dmTinhThanhEntity);

            lkTinhThanh.Properties.DataSource = lstTinhThanhEntity;
            lkTinhThanh.Properties.DisplayMember = "TenTinhThanh";
            lkTinhThanh.Properties.ValueMember = "MaTinhThanh";
            lkTinhThanh.EditValue = ConstantManagerClinic.sValueMember;
        }

        void LoadDmQuanHuyen()
        {
            var lstDmQuanHuyenEntity = dmQuanHuyenBusiness.GetDmQuanHuyenList();

            if (lstDmQuanHuyenEntity.Count > 0 && lkTinhThanh.EditValue.ToString() != ConstantManagerClinic.sValueMember)
            {
                lstDmQuanHuyenEntity = lstDmQuanHuyenEntity.Where(n => n.MaTinhThanh == lkTinhThanh.EditValue.ToString()).ToList();
            }

            var dmQuanHuyenEntity = new DmQuanHuyenEntity()
            {
                MaQuanHuyen = ConstantManagerClinic.sValueMember,
                TenQuanHuyen = ConstantManagerClinic.sDisplayMember
            };
            lstDmQuanHuyenEntity.Insert(0, dmQuanHuyenEntity);

            lkQuanHuyen.Properties.DataSource = lstDmQuanHuyenEntity;
            lkQuanHuyen.Properties.DisplayMember = "TenQuanHuyen";
            lkQuanHuyen.Properties.ValueMember = "MaQuanHuyen";
            lkQuanHuyen.EditValue = ConstantManagerClinic.sValueMember;
        }

        private void frmCapNhatDmBenhNhan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void frmCapNhatDmBenhNhan_Load(object sender, EventArgs e)
        {

        }

        private void lkQuocGia_EditValueChanged(object sender, EventArgs e)
        {
            LoadDmTinhThanh();
        }

        private void lkTinhThanh_EditValueChanged(object sender, EventArgs e)
        {
            LoadDmQuanHuyen();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidLookUpEdit(lkGioiTinh, "giới tính") == false) return;
            if (Validation.ValidLookUpEdit(lkDanToc, "dân tộc") == false) return;
            if (Validation.ValidLookUpEdit(lkQuocGia, "quốc gia") == false) return;
            if (Validation.ValidLookUpEdit(lkTinhThanh, "tỉnh thành") == false) return;
            if (Validation.ValidLookUpEdit(lkQuanHuyen, "quận huyện") == false) return;

            dmBenhNhanEntity = new DmBenhNhanEntity()
            {
                MaBenhNhan = txtMaBenhNhan.Text,
                TenBenhNhan = txtTenBenhNhan.Text,
                NgaySinh = txtNgaySinh.Text,
                ThangSinh = txtThangSinh.Text,
                NamSinh = Convert.ToInt32(txtNamSinh.Text),
                MaGioiTinh = lkGioiTinh.EditValue.ToString(),
                MaDanToc = lkDanToc.EditValue.ToString(),
                MaNgheNghiep = lkNgheNghiep.EditValue.ToString(),
                MaQuocGia = lkQuocGia.EditValue.ToString(),
                MaTinhThanh = lkTinhThanh.EditValue.ToString(),
                MaQuanHuyen = lkQuanHuyen.EditValue.ToString(),
                ThonPho = txtThonPho.Text,
                NoiLamViec = txtNoiLamViec.Text,
                SoDienThoai = txtDienThoai.Text,
                Email = txtEmail.Text
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                dmBenhNhanBusiness.InsertDmBenhNhan(dmBenhNhanEntity);
                this.Close();
                if (ButtonClicked != null) ButtonClicked(this, e);

                frmHoSoKhamBenh frmHoSoKhamBenh = new frmHoSoKhamBenh(txtMaBenhNhan.Text);
                frmHoSoKhamBenh.MdiParent = parentForm;
                frmHoSoKhamBenh.Show();
            }
            else
            {
                dmBenhNhanBusiness.EditDmBenhNhan(dmBenhNhanEntity);
                this.Close();
                if (ButtonClicked != null) ButtonClicked(this, e);

                frmHoSoKhamBenh frmHoSoKhamBenh = new frmHoSoKhamBenh(txtMaBenhNhan.Text);
                frmHoSoKhamBenh.MdiParent = parentForm;
                frmHoSoKhamBenh.Show();
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}